"""
1. 游戏编程：按以下要求定义一个乌龟类和鱼类并尝试编写游戏。（初学者不一定可以完整实现，但请务必先自己动手，你会从中学习到很多知识的^_^）
假设游戏场景为范围（x, y）为0<=x<=10，0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2（Ta可以随机选择1还是2移动），鱼儿的最大移动能力是1
当移动到场景边缘，自动向反方向移动
乌龟初始化体力为100（上限）
乌龟每移动一次，体力消耗1
当乌龟和鱼坐标重叠，乌龟吃掉鱼，乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0（挂掉）或者鱼儿的数量为0游戏结束
"""


import random


class Tortoise:
    direction = None
    step = None
    up_or_down = None
    tortoise_x_direction_step = 0
    tortoise_y_direction_step = 0

    def __init__(self):
        self.tortoise_strength = 100
        self.tortoise_num = 1

    def tortoise_move(self):
        # 移动方向随机
        self.direction = random.choice('xy')
        # 每次移动步数随机
        self.step = random.randint(1, 2)

    def calculate_tortoise_position(self):
        if (self.tortoise_x_direction_step == 10 or (self.tortoise_x_direction_step + self.step > 10)) and self.direction == 'x':
            # 自动向x轴的反方向移动
            self.up_or_down = 'down'
            self.tortoise_x_direction_step -= self.step
        elif (self.tortoise_y_direction_step == 10 or (self.tortoise_y_direction_step + self.step > 10)) and self.direction == 'y':
            # 自动向y轴的反方向移动
            self.up_or_down = 'down'
            self.tortoise_y_direction_step -= self.step
        elif (self.tortoise_x_direction_step == 0 or (self.tortoise_x_direction_step - self.step < 0)) and self.direction == 'x':
            # 自动向x轴的正方向移动
            self.up_or_down = "up"
            self.tortoise_x_direction_step += self.step
        elif (self.tortoise_y_direction_step == 0 or (self.tortoise_y_direction_step - self.step < 0)) and self.direction == 'y':
            # 自动向y轴的正方向移动
            self.up_or_down = "up"
            self.tortoise_y_direction_step += self.step
        else:
            # 乌龟移动不涉及到边界，移动方向没有限制
            self.up_or_down = random.choice(['up', 'down'])
            if self.direction == 'x' and self.up_or_down == "up":
                self.tortoise_x_direction_step += self.step
            elif self.direction == "x" and self.up_or_down == "down":
                self.tortoise_x_direction_step -= self.step
            elif self.direction == "y" and self.up_or_down == "up":
                self.tortoise_y_direction_step += self.step
            elif self.direction == "y" and self.up_or_down == "down":
                self.tortoise_y_direction_step -= self.step
        # 移动完之后乌龟的体力消耗1
        self.tortoise_strength -= 1


class Fish:
    direction = None
    step = None
    up_or_down = None
    fish_x_direction_step = 0
    fish_y_direction_step = 0

    def __init__(self):
        self.fish_num = 1

    def fish_move(self):
        # 移动方向随机
        self.direction = random.choice('xy')
        # 每次移动步数固定为1
        self.step = 1

    def calculate_fish_position(self):
        if self.fish_x_direction_step == 10 and self.direction == 'x':
            # 自动向x轴的反方向移动
            self.up_or_down = 'down'
            self.fish_x_direction_step -= self.step
        elif self.fish_y_direction_step == 10 and self.direction == 'y':
            # 自动向y轴的反方向移动
            self.up_or_down = 'down'
            self.fish_y_direction_step -= self.step
        elif self.fish_x_direction_step == 0 and self.direction == 'x':
            # 自动向x轴的正方向移动
            self.up_or_down = "up"
            self.fish_x_direction_step += self.step
        elif self.fish_y_direction_step == 0 and self.direction == 'y':
            # 自动向y轴的正方向移动
            self.up_or_down = "up"
            self.fish_y_direction_step += self.step
        else:
            # 鱼移动不涉及到边界，移动方向没有限制
            self.up_or_down = random.choice(['up', 'down'])
            if self.direction == 'x' and self.up_or_down == "up":
                self.fish_x_direction_step += self.step
            elif self.direction == "x" and self.up_or_down == "down":
                self.fish_x_direction_step -= self.step
            elif self.direction == "y" and self.up_or_down == "up":
                self.fish_y_direction_step += self.step
            elif self.direction == "y" and self.up_or_down == "down":
                self.fish_y_direction_step -= self.step


if __name__ == "__main__":
    # 实例化乌龟类
    tortoise = Tortoise()
    # 实例话鱼类
    fish = Fish()
    while True:
        # 计算乌龟移动之后的位置
        tortoise.tortoise_move()
        tortoise.calculate_tortoise_position()

        # 计算鱼移动之后的位置
        fish.fish_move()
        fish.calculate_fish_position()

        if tortoise.tortoise_strength == 0:
            print("乌龟体力耗尽，游戏结束")
            break
        elif (tortoise.tortoise_x_direction_step == fish.fish_x_direction_step) and \
                (tortoise.tortoise_y_direction_step == fish.fish_y_direction_step):
            # 乌龟体力回复20，鱼的数量-1
            tortoise.tortoise_strength += 20
            fish.fish_num -= 1
            if fish.fish_num == 0:
                print("鱼被吃完了，游戏结束")
                break

